
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>推荐系统学习：协同过滤实现 | Jark&#39;s Blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="WuChong">
    
    <meta name="description" itemprop="description" content="这篇文章主要总结下自己看了项亮的《推荐系统实践》后的学习笔记。作为入门，这么书确实写的不错。本文从评测指标、UserCF、ItemCF 的原理和实现以及比较来进行阐述。">
    
    
    <meta name="description" content="这篇文章主要总结下自己看了项亮的《推荐系统实践》后的学习笔记。作为入门，这么书确实写的不错。本文从评测指标、UserCF、ItemCF 的原理和实现以及比较来进行阐述。">
<meta property="og:type" content="article">
<meta property="og:title" content="推荐系统学习：协同过滤实现">
<meta property="og:url" content="http://wuchong.me/blog/2014/04/19/recsys-cf-study/">
<meta property="og:site_name" content="Jark's Blog">
<meta property="og:description" content="这篇文章主要总结下自己看了项亮的《推荐系统实践》后的学习笔记。作为入门，这么书确实写的不错。本文从评测指标、UserCF、ItemCF 的原理和实现以及比较来进行阐述。">
<meta property="og:image" content="http://ww2.sinaimg.cn/large/81b78497jw1efj1yg6uywj20kg0cm778.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/large/81b78497jw1efk99rahjcg205y0170sh.gif">
<meta property="og:image" content="http://ww3.sinaimg.cn/large/81b78497jw1efj1ygi0mej20im044757.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/large/81b78497jw1efk9i8h9cxg205v0170sh.gif">
<meta property="og:image" content="http://ww3.sinaimg.cn/large/81b78497jw1efj1yftjtvj20in044q3r.jpg">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="推荐系统学习：协同过滤实现">
<meta name="twitter:description" content="这篇文章主要总结下自己看了项亮的《推荐系统实践》后的学习笔记。作为入门，这么书确实写的不错。本文从评测指标、UserCF、ItemCF 的原理和实现以及比较来进行阐述。">
<meta name="twitter:creator" content="@jarkwu">
<link rel="publisher" href="111190881341800841449">

    
    <link rel="alternative" href="/atom.xml" title="Jark&#39;s Blog" type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/jacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/jacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css" type="text/css">
</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/logo.png" alt="Jark&#39;s Blog" title="Jark&#39;s Blog"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Jark&#39;s Blog">Jark&#39;s Blog</a></h1>
				<h2 class="blog-motto">当你的才华还撑不起你的野心时，你就应该静下心来学习。</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/">首页</a></li>
					
						<li><a href="/archives">归档</a></li>
					
						<li><a href="/about">关于</a></li>
					
					<li>
 					
						<form class="search" action="http://zhannei.baidu.com/cse/search" target="_blank">
							<label>Search</label>
						<input name="s" type="hidden" value= 783281470518440642 ><input type="text" name="q" size="30" placeholder="搜索"><br>
						</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/blog/2014/04/19/recsys-cf-study/" title="推荐系统学习：协同过滤实现" itemprop="url">推荐系统学习：协同过滤实现</a>
  </h1>
  <p class="article-author">By
       
		<a href="https://plus.google.com/111190881341800841449?rel=author" title="WuChong" target="_blank" itemprop="author">WuChong</a>
		
  <p class="article-time">
    <time datetime="2014-04-18T17:07:15.000Z" itemprop="datePublished"> 发表于 2014-04-19</time>
    
  </p>
</header>
	<div class="article-content">
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目录</strong>
		
			<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#推荐系统的评测指标"><span class="toc-number">1.</span> <span class="toc-text">推荐系统的评测指标</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#基于用户的协同过滤"><span class="toc-number">2.</span> <span class="toc-text">基于用户的协同过滤</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#UserCF_和_ItemCF_的区别和应用"><span class="toc-number">3.</span> <span class="toc-text">UserCF 和 ItemCF 的区别和应用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#参考文献"><span class="toc-number">4.</span> <span class="toc-text">参考文献</span></a></li></ol>
		
		</div>
		
		<p>阿里大数据竞赛的第一季也即将告一段落，之前一直想发篇竞赛心得/攻略，但是看到有人发的博客居然被阿里查封了就不敢了…扯远了，这篇文章主要总结下自己看了项亮的《推荐系统实践》后的学习笔记。作为入门，这么书确实写的不错。</p>
<h2 id="推荐系统的评测指标">推荐系统的评测指标</h2>
<p>为了评估推荐算法的好坏需要各方面的评估指标。</p>
<ul>
<li><p>准确率<br>准确率就是最终的推荐列表中有多少是推荐对了的。</p>
</li>
<li><p>召回率<br>召回率就是推荐对了的占全集的多少。<br>下图直观地描述了准确率和召回率的含义<br><img src="http://ww2.sinaimg.cn/large/81b78497jw1efj1yg6uywj20kg0cm778.jpg" alt=""></p>
<a id="more"></a></li>
<li><p>覆盖率<br>覆盖率表示推荐的物品占了物品全集空间的多大比例。</p>
</li>
<li><p>新颖度<br>新颖度是为了推荐长尾区间的物品。用推荐列表中物品的平均流行度度量推荐结果的新颖度。如果推荐出的物品都很热门，说明推荐的新颖度较低，否则说明推荐结果比较新颖。</p>
</li>
</ul>
<p>各个评测指标的代码实现如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre><span class="comment">#train为训练集合，test为验证集合，给每个用户推荐N个物品</span>
<span class="comment">#召回率和准确率</span>
<span class="function"><span class="keyword">def</span> <span class="title">RecallAndPrecision</span><span class="params">(self,train=None,test=None,K=<span class="number">3</span>,N=<span class="number">10</span>)</span>:</span>
    train = train <span class="keyword">or</span> self.train
    test = test <span class="keyword">or</span> self.test
    hit = <span class="number">0</span>
    recall = <span class="number">0</span>
    precision = <span class="number">0</span>
    <span class="keyword">for</span> user <span class="keyword">in</span> train.keys():
        tu = test.get(user,{})
        rank = self.Recommend(user,K=K,N=N)
        <span class="keyword">for</span> i,_ <span class="keyword">in</span> rank.items():
            <span class="keyword">if</span> i <span class="keyword">in</span> tu:
                hit += <span class="number">1</span>
        recall += len(tu)
        precision += N
    recall = hit / (recall * <span class="number">1.0</span>)
    precision = hit / (precision * <span class="number">1.0</span>)
    <span class="keyword">return</span> (recall,precision)

<span class="comment">#覆盖率</span>
<span class="function"><span class="keyword">def</span> <span class="title">Coverage</span><span class="params">(self,train=None,test=None,K=<span class="number">3</span>,N=<span class="number">10</span>)</span>:</span>
    train = train <span class="keyword">or</span> self.train
    recommend_items = set()
    all_items = set()
    <span class="keyword">for</span> user,items <span class="keyword">in</span> train.items():
        <span class="keyword">for</span> i <span class="keyword">in</span> items.keys():
            all_items.add(i)
        rank = self.Recommend(user,K)
        <span class="keyword">for</span> i,_ <span class="keyword">in</span> rank.items():
            recommend_items.add(i)
    <span class="keyword">return</span> len(recommend_items) / (len(all_items) * <span class="number">1.0</span>)

<span class="comment">#新颖度</span>
<span class="function"><span class="keyword">def</span> <span class="title">Popularity</span><span class="params">(self,train=None,test=None,K=<span class="number">3</span>,N=<span class="number">10</span>)</span>:</span>
    train = train <span class="keyword">or</span> self.train
    item_popularity = dict()
    <span class="comment">#计算物品流行度</span>
    <span class="keyword">for</span> user,items <span class="keyword">in</span> train.items():
        <span class="keyword">for</span> i <span class="keyword">in</span> items.keys():
            item_popularity.setdefault(i,<span class="number">0</span>)
            item_popularity[i] += <span class="number">1</span>

    ret = <span class="number">0</span>     <span class="comment">#新颖度结果</span>
    n = <span class="number">0</span>       <span class="comment">#推荐的总个数</span>
    <span class="keyword">for</span> user <span class="keyword">in</span> train.keys():
        rank = self.Recommend(user,K=K,N=N)    <span class="comment">#获得推荐结果</span>
        <span class="keyword">for</span> item,_ <span class="keyword">in</span> rank.items():
            ret += math.log(<span class="number">1</span> + item_popularity[item])
            n += <span class="number">1</span>
    ret /= n * <span class="number">1.0</span>
    <span class="keyword">return</span> ret
</pre></td></tr></table></figure>

<h2 id="基于用户的协同过滤">基于用户的协同过滤</h2>
<p>UserBasedCF的核心思想主要是找到和目标用户兴趣相似的用户集合，然后给目标用户推荐这个集合的用户喜欢的物品。关键在于计算用户与用户之间的兴趣相似度。这里主要使用余弦相似度来计算：</p>
<p>$$w_{uv} = \frac{|N(u) \cap N(v)|}{\sqrt{|N(u)|| N(v)|}}$$</p>
<p>$w_{uv}$ 代表用户 u 与 v 之间的兴趣相似度，$N(u)$表示用户 u 曾经喜欢过的物品集合, $N(v)$ 表示用户 v 曾经喜欢过的物品集合。</p>
<p>根据上述核心思想，可以有如下算法步骤：</p>
<ol>
<li>建立物品-用户的倒排表</li>
<li>用户与用户之间的共现矩阵 C[u][v]，表示用户u与v喜欢相同物品的个数</li>
<li>用户与用户之间的相似度矩阵 W[u][v]，根据上述相似度计算公式计算。</li>
<li>用上面的相似度矩阵来给用户推荐和他兴趣相似的用户喜欢的物品。用户 u 对物品 i 的兴趣程度可以估计为</li>
</ol>
<p><img src="http://ww1.sinaimg.cn/large/81b78497jw1efk99rahjcg205y0170sh.gif" alt=""></p>
<p>$S(u,K)$ 为和用户 u 兴趣最接近的 K 个用户， $N(i)$ 为对物品 i 有正反馈的用户集合， W[u][v] 为用户 u 和用户 v 的兴趣相似度，$r_{vi}$ 为用户 v 对物品 i 的兴趣。</p>
<p>下面是UserBasedCF的代码实现：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
</pre></td><td class="code"><pre><span class="class"><span class="keyword">class</span> <span class="title">UserBasedCF</span>:</span>
    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,train_file,test_file)</span>:</span>
        self.train_file = train_file
        self.test_file = test_file
        self.readData()
    <span class="function"><span class="keyword">def</span> <span class="title">readData</span><span class="params">(self)</span>:</span>
        <span class="comment">#读取文件，并生成用户-物品的评分表和测试集</span>
        self.train = dict()     <span class="comment">#用户-物品的评分表</span>
        <span class="keyword">for</span> line <span class="keyword">in</span> open(self.train_file):
            <span class="comment"># user,item,score = line.strip().split(",")</span>
            user,item,score,_ = line.strip().split(<span class="string">"\t"</span>)
            self.train.setdefault(user,{})
            self.train[user][item] = int(score)
        self.test = dict()      <span class="comment">#测试集</span>
        <span class="keyword">for</span> line <span class="keyword">in</span> open(self.test_file):
            <span class="comment"># user,item,score = line.strip().split(",")</span>
            user,item,score,_ = line.strip().split(<span class="string">"\t"</span>)
            self.test.setdefault(user,{})
            self.test[user][item] = int(score)


    <span class="function"><span class="keyword">def</span> <span class="title">UserSimilarity</span><span class="params">(self)</span>:</span>
        <span class="comment">#建立物品-用户的倒排表</span>
        self.item_users = dict()
        <span class="keyword">for</span> user,items <span class="keyword">in</span> self.train.items():
            <span class="keyword">for</span> i <span class="keyword">in</span> items.keys():
                <span class="keyword">if</span> i <span class="keyword">not</span> <span class="keyword">in</span> self.item_users:
                    self.item_users[i] = set()
                self.item_users[i].add(user)

        <span class="comment">#计算用户-用户相关性矩阵</span>
        C = dict()  <span class="comment">#用户-用户共现矩阵</span>
        N = dict()  <span class="comment">#用户产生行为的物品个数</span>
        <span class="keyword">for</span> i,users <span class="keyword">in</span> self.item_users.items():
            <span class="keyword">for</span> u <span class="keyword">in</span> users:
                N.setdefault(u,<span class="number">0</span>)
                N[u] += <span class="number">1</span>
                C.setdefault(u,{})
                <span class="keyword">for</span> v <span class="keyword">in</span> users:
                    <span class="keyword">if</span> u == v:
                        <span class="keyword">continue</span>
                    C[u].setdefault(v,<span class="number">0</span>)
                    C[u][v] += <span class="number">1</span>

        <span class="comment">#计算用户-用户相似度，余弦相似度</span>
        self.W = dict()      <span class="comment">#相似度矩阵</span>
        <span class="keyword">for</span> u,related_users <span class="keyword">in</span> C.items():
            self.W.setdefault(u,{})
            <span class="keyword">for</span> v,cuv <span class="keyword">in</span> related_users.items():
                self.W[u][v] = cuv / math.sqrt(N[u] * N[v])
        <span class="keyword">return</span> self.W

    <span class="comment">#给用户user推荐，前K个相关用户</span>
    <span class="function"><span class="keyword">def</span> <span class="title">Recommend</span><span class="params">(self,user,K=<span class="number">3</span>,N=<span class="number">10</span>)</span>:</span>
        rank = dict()
        action_item = self.train[user].keys()     <span class="comment">#用户user产生过行为的item</span>
        <span class="keyword">for</span> v,wuv <span class="keyword">in</span> sorted(self.W[user].items(),key=<span class="keyword">lambda</span> x:x[<span class="number">1</span>],reverse=<span class="keyword">True</span>)[<span class="number">0</span>:K]:
            <span class="comment">#遍历前K个与user最相关的用户</span>
            <span class="keyword">for</span> i,rvi <span class="keyword">in</span> self.train[v].items():
                <span class="keyword">if</span> i <span class="keyword">in</span> action_item:
                    <span class="keyword">continue</span>
                rank.setdefault(i,<span class="number">0</span>)
                rank[i] += wuv * rvi
        <span class="keyword">return</span> dict(sorted(rank.items(),key=<span class="keyword">lambda</span> x:x[<span class="number">1</span>],reverse=<span class="keyword">True</span>)[<span class="number">0</span>:N])   <span class="comment">#推荐结果的取前N个</span>
</pre></td></tr></table></figure><br>采用 MovieLens 数据集对 UserCF 算法测试之后各评测指标的结果如下<br><img src="http://ww3.sinaimg.cn/large/81b78497jw1efj1ygi0mej20im044757.jpg" alt=""><br><br>##基于物品的协同过滤<br>ItemBasedCF 应该是业界的应用最广泛的推荐算法了。该算法的核心思想主要是：给目标用户推荐与他喜欢的物品相似度较高高的物品。我们经常在京东、天猫上看到「购买了该商品的用户也经常购买的其他商品」，就是主要基于 ItemBasedCF。一般我们先计算物品之间的相似度，然后根据物品的相似度和用户的历史行为给用户生成推荐列表。<br><br>物品 i 和 j 之间的相似度可以使用如下公式计算：<br><br>$$w<em>{ij} = \frac{|N(i) \cap N(j)|}{\sqrt{|N(i)|| N(j)|}}$$<br><br>从上面的定义可以看到，在协同过滤中两个物品产生相似度是因为它们共同被很多用户喜欢，也就是说每个用户都可以通过他们的历史兴趣列表给物品“贡献”相似度。<br><br>根据上述核心思想，可以有如下算法步骤：<br>1. 建立用户-物品的倒排表<br>2. 物品与物品之间的共现矩阵 C[i][j]，表示物品 i 与 j 共同被多少用户所喜欢。<br>3. 用户与用户之间的相似度矩阵 W[i][j] ， 根据上述相似度计算公式计算。<br>4. 用上面的相似度矩阵来给用户推荐与他所喜欢的物品相似的其他物品。用户 u 对物品 j 的兴趣程度可以估计为<br><br><img src="http://ww1.sinaimg.cn/large/81b78497jw1efk9i8h9cxg205v0170sh.gif" alt=""><br><br>$S(j,K)$ 为和物品 j 最相似的前 K 个物品， $N(u)$ 为对用户 u 所喜欢的物品集合， W[j][i] 为物品 j 和物品 i 之间的相似度， $r</em>{ui}$ 为用户 u 对物品 i 的兴趣。<br><br>下面是ItemBasedCF 的代码实现：<br><figure class="highlight python"><table><tr><td class="gutter"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre><span class="class"><span class="keyword">class</span> <span class="title">ItemBasedCF</span>:</span>
    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,train_file,test_file)</span>:</span>
        self.train_file = train_file
        self.test_file = test_file
        self.readData()
    <span class="function"><span class="keyword">def</span> <span class="title">readData</span><span class="params">(self)</span>:</span>
        <span class="comment">#读取文件，并生成用户-物品的评分表和测试集</span>
        self.train = dict()     <span class="comment">#用户-物品的评分表</span>
        <span class="keyword">for</span> line <span class="keyword">in</span> open(self.train_file):
            <span class="comment"># user,item,score = line.strip().split(",")</span>
            user,item,score,_ = line.strip().split(<span class="string">"\t"</span>)
            self.train.setdefault(user,{})
            self.train[user][item] = int(score)
        self.test = dict()      <span class="comment">#测试集</span>
        <span class="keyword">for</span> line <span class="keyword">in</span> open(self.test_file):
            <span class="comment"># user,item,score = line.strip().split(",")</span>
            user,item,score,_ = line.strip().split(<span class="string">"\t"</span>)
            self.test.setdefault(user,{})
            self.test[user][item] = int(score)

    <span class="function"><span class="keyword">def</span> <span class="title">ItemSimilarity</span><span class="params">(self)</span>:</span>
        <span class="comment">#建立物品-物品的共现矩阵</span>
        C = dict()  <span class="comment">#物品-物品的共现矩阵</span>
        N = dict()  <span class="comment">#物品被多少个不同用户购买</span>
        <span class="keyword">for</span> user,items <span class="keyword">in</span> self.train.items():
            <span class="keyword">for</span> i <span class="keyword">in</span> items.keys():
                N.setdefault(i,<span class="number">0</span>)
                N[i] += <span class="number">1</span>
                C.setdefault(i,{})
                <span class="keyword">for</span> j <span class="keyword">in</span> items.keys():
                    <span class="keyword">if</span> i == j : <span class="keyword">continue</span>
                    C[i].setdefault(j,<span class="number">0</span>)
                    C[i][j] += <span class="number">1</span>
        <span class="comment">#计算相似度矩阵</span>
        self.W = dict()
        <span class="keyword">for</span> i,related_items <span class="keyword">in</span> C.items():
            self.W.setdefault(i,{})
            <span class="keyword">for</span> j,cij <span class="keyword">in</span> related_items.items():
                self.W[i][j] = cij / (math.sqrt(N[i] * N[j]))
        <span class="keyword">return</span> self.W

    <span class="comment">#给用户user推荐，前K个相关用户</span>
    <span class="function"><span class="keyword">def</span> <span class="title">Recommend</span><span class="params">(self,user,K=<span class="number">3</span>,N=<span class="number">10</span>)</span>:</span>
        rank = dict()
        action_item = self.train[user]     <span class="comment">#用户user产生过行为的item和评分</span>
        <span class="keyword">for</span> item,score <span class="keyword">in</span> action_item.items():
            <span class="keyword">for</span> j,wj <span class="keyword">in</span> sorted(self.W[item].items(),key=<span class="keyword">lambda</span> x:x[<span class="number">1</span>],reverse=<span class="keyword">True</span>)[<span class="number">0</span>:K]:
                <span class="keyword">if</span> j <span class="keyword">in</span> action_item.keys():
                    <span class="keyword">continue</span>
                rank.setdefault(j,<span class="number">0</span>)
                rank[j] += score * wj
        <span class="keyword">return</span> dict(sorted(rank.items(),key=<span class="keyword">lambda</span> x:x[<span class="number">1</span>],reverse=<span class="keyword">True</span>)[<span class="number">0</span>:N])
</pre></td></tr></table></figure>

<p>采用 MovieLens 数据集对 ItemCF 算法测试之后各评测指标的结果如下<br><img src="http://ww3.sinaimg.cn/large/81b78497jw1efj1yftjtvj20in044q3r.jpg" alt=""></p>
<h2 id="UserCF_和_ItemCF_的区别和应用">UserCF 和 ItemCF 的区别和应用</h2>
<p>UserCF 算法的特点是：</p>
<ul>
<li>用户较少的场合，否则用户相似度矩阵计算代价很大</li>
<li>适合时效性较强，用户个性化兴趣不太明显的领域</li>
<li>对新用户不友好，对新物品友好，因为用户相似度矩阵不能实时计算</li>
<li>很难提供令用户信服的推荐解释</li>
</ul>
<p>对应地，ItemCF 算法的特点：</p>
<ul>
<li>适用于物品数明显小于用户数的场合，否则物品相似度矩阵计算代价很大</li>
<li>适合长尾物品丰富，用户个性化需求强的领域</li>
<li>对新用户友好，对新物品不友好，因为物品相似度矩阵不需要很强的实时性</li>
<li>利用用户历史行为做推荐解释，比较令用户信服</li>
</ul>
<p>因此，可以看出 UserCF 适用于物品增长很快，实时性较高的场合，比如新闻推荐。而在图书、电子商务和电影领域，比如京东、天猫、优酷中，ItemCF 则能极大地发挥优势。在这些网站中，用户的兴趣是比较固定和持久的，而且这些网站的物品更新速度不会特别快，一天一更新是在忍受范围内的。</p>
<h2 id="参考文献">参考文献</h2>
<ul>
<li>项亮 <a href="http://book.douban.com/subject/10769749/" target="_blank" rel="external">《推荐系统实践》</a></li>
<li><a href="http://www.oschina.net/code/snippet_244322_15369" target="_blank" rel="external">基于用户的协同过滤算法</a></li>
</ul>
  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/推荐系统/">推荐系统</a>
</div>


  <div class="article-tags">
  
  <span></span> <a href="/tags/协同过滤/">协同过滤</a>
  </div>

</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://wuchong.me/blog/2014/04/19/recsys-cf-study/" data-title="推荐系统学习：协同过滤实现 | Jark&#39;s Blog" data-tsina="2176287895" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/blog/2014/04/24/easy-web-scraping-with-python/" title="使用 Python 轻松抓取网页">
  <strong>上一篇：</strong><br/>
  <span>
  使用 Python 轻松抓取网页</span>
</a>
</div>


<div class="next">
<a href="/blog/2014/03/25/interview-link-questions/"  title="面试精选：链表问题集锦">
 <strong>下一篇：</strong><br/> 
 <span>面试精选：链表问题集锦
</span>
</a>
</div>

</nav>

	
<section id="comments" class="comment">
	<div class="ds-thread" data-thread-key="blog/2014/04/19/recsys-cf-study/" data-title="推荐系统学习：协同过滤实现" data-url="http://wuchong.me/blog/2014/04/19/recsys-cf-study/"></div>
</section>


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  
<div class="categorieslist">
	<p class="asidetitle">分类</p>
		<ul>
		
			<li><a href="/categories/Hexo/" title="Hexo">Hexo<sup>7</sup></a></li>
		
			<li><a href="/categories/LinuxUnix/" title="Linux/Unix">Linux/Unix<sup>6</sup></a></li>
		
			<li><a href="/categories/Python/" title="Python">Python<sup>3</sup></a></li>
		
			<li><a href="/categories/推荐系统/" title="推荐系统">推荐系统<sup>3</sup></a></li>
		
			<li><a href="/categories/杂项资源/" title="杂项资源">杂项资源<sup>2</sup></a></li>
		
			<li><a href="/categories/程序设计/" title="程序设计">程序设计<sup>5</sup></a></li>
		
			<li><a href="/categories/系统架构/" title="系统架构">系统架构<sup>1</sup></a></li>
		
			<li><a href="/categories/编程语言/" title="编程语言">编程语言<sup>2</sup></a></li>
		
			<li><a href="/categories/职场生涯/" title="职场生涯">职场生涯<sup>2</sup></a></li>
		
			<li><a href="/categories/随笔生活/" title="随笔生活">随笔生活<sup>5</sup></a></li>
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">标签</p>
		<ul class="clearfix">
		
			<li><a href="/tags/Hexo/" title="Hexo">Hexo<sup>8</sup></a></li>
		
			<li><a href="/tags/面试/" title="面试">面试<sup>5</sup></a></li>
		
			<li><a href="/tags/Linux基础/" title="Linux基础">Linux基础<sup>5</sup></a></li>
		
			<li><a href="/tags/生活/" title="生活">生活<sup>4</sup></a></li>
		
			<li><a href="/tags/博客/" title="博客">博客<sup>4</sup></a></li>
		
			<li><a href="/tags/Python/" title="Python">Python<sup>4</sup></a></li>
		
			<li><a href="/tags/阿里推荐大赛/" title="阿里推荐大赛">阿里推荐大赛<sup>4</sup></a></li>
		
			<li><a href="/tags/算法/" title="算法">算法<sup>2</sup></a></li>
		
			<li><a href="/tags/爬虫/" title="爬虫">爬虫<sup>2</sup></a></li>
		
			<li><a href="/tags/ODPS/" title="ODPS">ODPS<sup>2</sup></a></li>
		
			<li><a href="/tags/教程/" title="教程">教程<sup>2</sup></a></li>
		
			<li><a href="/tags/Jacman/" title="Jacman">Jacman<sup>2</sup></a></li>
		
			<li><a href="/tags/Java/" title="Java">Java<sup>2</sup></a></li>
		
			<li><a href="/tags/文件系统/" title="文件系统">文件系统<sup>2</sup></a></li>
		
			<li><a href="/tags/启动流程/" title="启动流程">启动流程<sup>1</sup></a></li>
		
			<li><a href="/tags/权限/" title="权限">权限<sup>1</sup></a></li>
		
			<li><a href="/tags/正则表达式/" title="正则表达式">正则表达式<sup>1</sup></a></li>
		
			<li><a href="/tags/jQuery/" title="jQuery">jQuery<sup>1</sup></a></li>
		
			<li><a href="/tags/双检锁/" title="双检锁">双检锁<sup>1</sup></a></li>
		
			<li><a href="/tags/设计模式/" title="设计模式">设计模式<sup>1</sup></a></li>
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">友情链接</p>
    <ul>
        
          <li>
            <a href="https://coderq.com" target="_blank" title="码农圈">码农圈</a>
          </li>
        
          <li>
            <a href="http://zipperary.com/" target="_blank" title="Zippera&#39;s Blog">Zippera&#39;s Blog</a>
          </li>
        
          <li>
            <a href="http://hijiangtao.github.io/" target="_blank" title="Data.Blog">Data.Blog</a>
          </li>
        
    </ul>
</div>

  <div class="weiboshow">
  <p class="asidetitle">新浪微博</p>
    <iframe width="100%" height="119" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=119&fansRow=2&ptype=1&speed=0&skin=9&isTitle=1&noborder=1&isWeibo=0&isFans=0&uid=2176287895&verifier=b3593ceb&dpc=1"></iframe>
</div>


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello,I&#39;m WuChong. For now I&#39;m a graduate student in Beijing. <br/>
			I&#39;ll share my learning experience with you at this blog.</p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		<a href="http://weibo.com/wuchong1014" target="_blank" class="icon-weibo" title="微博"></a>
		
		
		<a href="https://github.com/wuchong" target="_blank" class="icon-github" title="github"></a>
		
		
		
		<a href="https://twitter.com/jarkwu" target="_blank" class="icon-twitter" title="twitter"></a>
		
		
		
		
		
		
		<a href="https://plus.google.com/111190881341800841449?rel=author" target="_blank" class="icon-google_plus" title="Google+"></a>
		
		
	</div>
		<p class="copyright">Powered by <a href="http://zespia.tw/hexo/" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wuchong/jacman" target="_blank" title="Pacman">Jacman</a> © 2014 
		
		<a href="http://wuchong.me/about" target="_blank" title="WuChong">WuChong</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/jquery.imagesloaded.min.js"></script>
<script src="/js/gallery.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<a href="#" class="overlay" id="qrcode"></a>',
  '<div class="qrcode clearfix"><span>扫描二维码分享到微信朋友圈</span><a class="qrclose" href="#nothing"></a><strong>Loading...Please wait</strong><img id="qrcode-pic" data-src="http://s.jiathis.com/qrcode.php?url=' + encodedUrl + '"/></div>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);
  $('.article-share-qrcode').click(function(){
    var imgSrc = $('#qrcode-pic').attr('data-src');
    $('#qrcode-pic').attr('src', imgSrc);
    $('#qrcode-pic').load(function(){
        $('.qrcode strong').text(' ');
    });
  });
});     
</script>



<script type="text/javascript">
  var duoshuoQuery = {short_name:"wuchong"};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0] 
    || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
</script> 







<link rel="stylesheet" href="/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->

<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-46321946-2', 'wuchong.me');  
ga('send', 'pageview');
</script>





<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="返回顶部"><img src="/img/scrollup.png"/></a>
	</div>
	<script src="/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

  </body>
</html>
